<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/24/024
 * Time: 22:38
 */
namespace app\api\model;
use think\Model;
use think\facade\Url;
use phpqrcode\QRcode;
use app\api\Model\Area;

class MemberProfile extends Model
{
    const SEX = [1, 2, 3]; //男，女，保密
    
    /**
    * 会员注册时创建会员资料
    * @param mid int 会员id
    */
    public function _addProfile($mid, $image = '',$nick_name=""){
        $data = $this->where([['mid', '=', $mid]])->find();
        if(!empty($data)){
            $this->error = '该用户信息已存在';
            return false;
        }
        $time = time();
        if(!$nick_name){
            $nick_name = rand_name();
        }
        $insert_data = [
            'mid' => $mid,
            'nick_name' => $nick_name,
            'image' => $image,
            'updated' => $time,
            'created' => $time,
        ];
        $ret = $this->save($insert_data);
        if(!$ret){
            $this->error = '用户资料创建失败';
            return false;
        }
        
        return true;
    }
    
    /**
    *   设置用户头像
    * @param mid int 用户id
    * @param image 用户头像
    * @return bool
    */
    public function _setImage($mid, $image){
        $profile = $this->where([['mid', '=', $mid]])->find();
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        $profile->image = $image;
        $profile->updated = time();
        $ret = $profile->save();
        if($ret === false){
            $this->error = '头像设置失败';
            return false;
        }
        
        return true;
    }
    
    /**
    *   设置用户昵称
    * @param mid int 用户id
    * @param nick_name 用户昵称
    * @return bool
    */
    public function _setNickName($mid, $nick_name){
        $profile = $this->where([['mid', '=', $mid]])->find();
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        $profile->nick_name = $nick_name;
        $profile->updated = time();
        $ret = $profile->save();
        if($ret === false){
            $this->error = '昵称设置失败';
            return false;
        }
        
        return true;
    }

    /**
    *   设置用户性别
    * @param mid int 用户id
    * @param sex 用户性别 1男 2女
    * @return bool
    */
    public function _setSex($mid, $sex){
        $profile = $this->where([['mid', '=', $mid]])->find();
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        if(!in_array($sex, self::SEX)){
            $this->error = '性别异常';
            return false;
        }
        $profile->sex = $sex;
        $profile->updated = time();

        $ret = $profile->save();
        if($ret === false){
            $this->error = '性别设置失败';
            return false;
        }
        
        return true;
    }
    
    /**
    *   设置用户邮箱
    * @param mid int 用户id
    * @param email 用户邮箱
    * @return bool
    */
    public function _setEmail($mid, $email){
        $profile = $this->where([['mid', '=', $mid]])->find();
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        $pattern = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/";
        if(!preg_match($pattern, $email)){
            $this->error = '请输入合法的邮箱';
            return false;
        }
        
        $profile->email = $email;
        $profile->updated = time();

        $ret = $profile->save();
        if($ret === false){
            $this->error = '邮箱设置失败';
            return false;
        }
        
        return true;
    }
    
    /**
    *   设置用户生日
    * @param mid int 用户id
    * @param birth 用户生日 年/月/日
    * @return bool
    */
    public function _setBirth($mid, $birth){
        $profile = $this->where([['mid', '=', $mid]])->find();
       
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        $profile->birthday = $birth;
        $profile->updated = time();
        $profile->birthdayint = strtotime($birth);
        $ret = $profile->save();
        if($ret === false){
            $this->error = '生日设置失败';
            return false;
        }
        
        return true;
    }
    
    /**
    *   设置用户地区
    * @param mid int 用户id
    * @param pid 用户省级id
    * @param cid 用户市级id
    * @param aid 用户区县id
    * @return bool
    */
    public function _setAddress($mid, $pid, $cid, $aid){
       
        $profile = $this->where([['mid', '=', $mid]])->find();
        
        if(empty($profile)){
            $this->error = '用户资料不存在';
            return false;
        }
        
        if(empty($pid) || empty($cid)){
            $this->error = '地址ID错误';
            return false;
        }
        $profile->pid = $pid;
        $profile->cid = $cid;
        $profile->aid = $aid;
        $profile->updated = time();
        $area_model = new Area;
        $pname = $area_model->_getAreaById($pid);
        $cname = $area_model->_getAreaById($cid);
        $aname = '';
        
        if(!empty($aid)){
            $aname = $area_model->_getAreaById($aid);
        }
        $address = $pname.$cname.$aname;
        $profile->address = $address;
        $ret = $profile->save();
        if($ret === false){
            $this->error = '地区设置失败';
            return false;
        }
        
        return true;
    }
    

    
    /**
    * 通过mid获取用户资料
    * @param mid int 会员id
    * @return array
    */
    public function _getByMid($mid){
        $data = $this->where([['mid', '=', $mid]])->find();
        return $data;
    }

    /**
    * 校验是否实名认证
    * @param mid int 会员id
    * @return bool
    */
    public function _checkRealName($mid){
        $data = $this->_getByMid($mid);
        return !empty($data['real_name']);
    }
    

    /**
    * 更新真实姓名
    * @param mid int 会员id
    * @param real_name string 真实姓名
    */
    public function _updateRealName($mid, $real_name){
        $profile = $this->_getByMid($mid);
        $profile->real_name = $real_name;
        $ret = $profile->save();
        if(!$ret){
            $this->error = '真实姓名更新失败';
            return false;
        }
        
        return true;
    }
}